home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Sample Code / Snippets / QuickTime / Decompression & Scaling / Compression & Scaling.c next >
Encoding:
C/C++ Source or Header  |  1992-11-16  |  4.7 KB  |  170 lines  |  [TEXT/KAHL]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    Compression & Scaling                                    */
  4. /*                                                                            */
  5. /*    Description:    This snippet shows an example of how to convert a        */
  6. /*                    version 2 PICT to a compressed QuickTime data buffer.    */
  7. /*                    Once the PICT is compressed, it is then decompressed    */
  8. /*                    to the window at one quarter its original size.            */
  9. /*                                                                            */
  10. /*    Files:            Compression & Scaling.π                                    */
  11. /*                    Compression & Scaling.π.rsrc                            */
  12. /*                    Compression & Scaling.c                                    */
  13. /*                                                                            */
  14. /*    Programmer:        Edgar Lee                                                */
  15. /*    Organization:    Apple Computer, Inc.                                    */
  16. /*    Department:        Developer Technical Support, DTS                        */
  17. /*    Language:        C (Think C version 5.0.2)                                */
  18. /*    Date Created:    10-20-92                                                */
  19. /*                                                                            */
  20. /****************************************************************************/
  21.  
  22. #include <ImageCompression.h>
  23. #include <Movies.h>
  24.  
  25. /* Constant Declarations */
  26.  
  27. #define WLEFT( x )    (((screenBits.bounds.right - screenBits.bounds.left) - x) / 2)
  28. #define WTOP( x )    (((screenBits.bounds.bottom - screenBits.bounds.top) - x) / 2)
  29.  
  30. /* Global Variable Definitions */
  31.  
  32. WindowPtr        gWindow;
  33.  
  34. void    initMac();
  35. void    checkForQuickTime();
  36. void    doScaleTest();
  37. void    createWindow();
  38.  
  39. main()
  40. {
  41.     initMac();
  42.     checkForQuickTime();
  43.     
  44.     createWindow();
  45.     doScaleTest();
  46.     
  47.     while (!Button());
  48. }
  49.  
  50. void initMac()
  51. {
  52.     InitGraf( &thePort );
  53.     InitFonts();
  54.     InitWindows();
  55.     InitMenus();
  56.     TEInit();
  57.     InitDialogs( nil );
  58.     InitCursor();
  59.     FlushEvents( 0, everyEvent );
  60. }
  61.  
  62. void checkForQuickTime()
  63. {
  64.     long    version;
  65.     
  66.     if (Gestalt( gestaltQuickTime, &version ) != noErr)
  67.     {
  68.         ParamText( "\pQuickTime not installed.  Please install, then try again.", "\p", "\p", "\p" );
  69.         Alert( 128, nil );
  70.         ExitToShell();
  71.     }
  72. }
  73.  
  74. void createWindow()
  75. {
  76.     Rect    bounds = { 0, 0, 0, 0 };
  77.     
  78.     gWindow = NewCWindow( 0L, &bounds, "\pDecompression & Scaling", false, documentProc,
  79.                             (WindowPtr)-1L, false, 0L );
  80.                             
  81.     SetPort( gWindow );
  82. }
  83.  
  84. void doScaleTest()
  85. {
  86.     int                i;
  87.     GWorldPtr        srcGWorld;
  88.     PixMapHandle    srcPixMap;
  89.     PicHandle        pict;
  90.     Rect            pictBounds;
  91.     Rect            finalBounds;
  92.     CGrafPtr        savedPort;
  93.     GDHandle        savedDevice;
  94.     short            scaleRatio = 4;
  95.     
  96.     ImageDescriptionHandle    desc;
  97.     Ptr                        imageData;
  98.     long                    size;
  99.     
  100.     /**********************************************************/
  101.     /* Load the picture and define the source and dest rects. */
  102.     /**********************************************************/
  103.  
  104.     pict = GetPicture( 128 );
  105.     pictBounds = (**pict).picFrame;
  106.     OffsetRect( &pictBounds, -pictBounds.left, -pictBounds.top );
  107.     
  108.     finalBounds = pictBounds;
  109.     finalBounds.right = finalBounds.right / scaleRatio;
  110.     finalBounds.bottom = finalBounds.bottom / scaleRatio;
  111.     
  112.     SizeWindow( gWindow, pictBounds.right + finalBounds.right,
  113.                             pictBounds.bottom, false );
  114.     MoveWindow( gWindow, WLEFT( pictBounds.right + finalBounds.right ),
  115.                             WTOP( pictBounds.bottom ), false );
  116.     ShowWindow( gWindow );
  117.     
  118.     /********************************************************/
  119.     /* Create a temporary offscreen used to store the pict. */
  120.     /********************************************************/
  121.     
  122.     if (NewGWorld( &srcGWorld, 8, &pictBounds, GetCTable( 8 ), nil, 0 ) != noErr)
  123.         ExitToShell();
  124.         
  125.     srcPixMap = GetGWorldPixMap( srcGWorld );    
  126.     LockPixels( srcPixMap );
  127.     
  128.     /********************************************************/
  129.     /* Draw the picture into the temporary gworld & window. */
  130.     /********************************************************/
  131.     
  132.     GetGWorld( &savedPort, &savedDevice );
  133.     SetGWorld( srcGWorld, nil );
  134.     DrawPicture( pict, &pictBounds );
  135.     
  136.     SetGWorld( savedPort, savedDevice );
  137.     DrawPicture( pict, &pictBounds );
  138.     
  139.     /*************************************************/
  140.     /* Now, compress the picture into a data buffer, */
  141.     /*   then dispose the temporary gworld.          */
  142.     /*************************************************/
  143.     
  144.     if (GetMaxCompressionSize( srcPixMap, &(**srcPixMap).bounds, 8,
  145.                         codecMaxQuality, 'raw ', bestSpeedCodec, &size ))
  146.         ExitToShell();
  147.                             
  148.     imageData = NewPtr( size );
  149.     desc = NewHandle( 1 );
  150.     
  151.     if (CompressImage( srcPixMap, &(**srcPixMap).bounds, codecMaxQuality,
  152.                     'raw ', desc, imageData ))
  153.         ExitToShell();
  154.     
  155.     DisposeGWorld( srcGWorld );
  156.     
  157.     /**********************************************************/
  158.     /* Decompress the data buffer to the window with scaling. */
  159.     /**********************************************************/
  160.  
  161.     OffsetRect( &finalBounds, pictBounds.right, 0 );
  162.     
  163.     for (i = 0; i < scaleRatio; i++)
  164.     {        
  165.         DecompressImage( imageData, desc, (*(CWindowPtr)gWindow).portPixMap, 
  166.                     &pictBounds, &finalBounds, srcCopy + ditherCopy, nil );
  167.         
  168.         OffsetRect( &finalBounds, 0, finalBounds.bottom - finalBounds.top );
  169.     }            
  170. }